package com.example.shixunandjavagotoyangchen.Eternity.YinDaoActivity;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.example.modelviewpresenter.View.BasterActivity;
import com.example.shixunandjavagotoyangchen.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
@Route(path = "/start/yindao")
public class YinDaoActivity extends BasterActivity {

    int shusum=0;
    Timer timer;
    BlankFragmentYindao3 blankFragmentYindao3;
    List<Fragment> list;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 100) {

                if (shusum == list.size()) {
                    timer.cancel();//停止轮播
                }else {
                    yindaoViewPager.setCurrentItem(shusum++);
                }

            }

        }
    };

    private androidx.viewpager.widget.ViewPager yindaoViewPager;
    private android.widget.ImageView yindaoImg1;
    private android.widget.ImageView yindaoImg2;
    private android.widget.ImageView yindaoImg3;

    @Override
    public int BandLayout() {
        return R.layout.activity_yin_dao;
    }

    @Override
    public void initView() {
        onWindowFocusChanged(true);
        yindaoViewPager = (ViewPager) findViewById(R.id.yindao_ViewPager);
        yindaoImg1 = (ImageView) findViewById(R.id.yindao_img1);
        yindaoImg2 = (ImageView) findViewById(R.id.yindao_img2);
        yindaoImg3 = (ImageView) findViewById(R.id.yindao_img3);
    }

    @Override
    public void initDate() {
        Intent intent = getIntent();
        String start = intent.getStringExtra("start");
        ToastUtils.showShort(""+start);
        list = new ArrayList<>();
        list.add(new BlankFragmentYindao1());
        list.add(new BlankFragmentYindao2());
        blankFragmentYindao3 = new BlankFragmentYindao3();
        list.add(blankFragmentYindao3);

        FragmentBlank fragmentBlank = new FragmentBlank(getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,list);
        yindaoViewPager.setAdapter(fragmentBlank);

        //每三秒自动切换
        timer = new Timer();

        timer.schedule(new TimerTask() {
            @Override
            public void run() {  //子线程里不可以更新UI
                handler.sendEmptyMessage(100);
            }
        }, 0, 3000);


        yindaoViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        yindaoImg1.setImageResource(R.drawable.cricle_true);
                        yindaoImg2.setBackground(getResources().getDrawable(R.drawable.cricle_false));
                        yindaoImg3.setBackground(getResources().getDrawable(R.drawable.cricle_false));
                        break;

                    case 1:
                        yindaoImg1.setBackground(getResources().getDrawable(R.drawable.cricle_false));
                        yindaoImg2.setBackground(getResources().getDrawable(R.drawable.cricle_true));
                        yindaoImg3.setBackground(getResources().getDrawable(R.drawable.cricle_false));
                        break;


                    case 2:
                        yindaoImg1.setBackground(getResources().getDrawable(R.drawable.cricle_false));
                        yindaoImg2.setBackground(getResources().getDrawable(R.drawable.cricle_false));
                        yindaoImg3.setBackground(getResources().getDrawable(R.drawable.cricle_true));
                        timer.cancel();  //停止轮播
                        //通知fragment3开始倒计时
                        blankFragmentYindao3.handler.sendEmptyMessageDelayed(150712,2000);

                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }



}
